﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.IO;
using System.Xml;
using System.Runtime.Serialization;


namespace ClientApplication
{   
    [DataContract]
    [Serializable]
    public class ContestantList
    {
        [DataMember]
        public List<Contestant> contestants { get; set; }

        public ContestantList()
        {
            this.contestants = new List<Contestant>();
        }

        public void AddContestants(Contestant c)
        {
            contestants.Add(c);
        }
        public Contestant this[int i]
        {
            get
            {
                if (contestants.Count > 0)
                    return contestants[i];
                else
                    return null;
            }
        }
        public void SortList()
        {
            double c1 = 0;
            double c2 = 0;
            for(int i = 0; i < contestants.Count; i++)
            {
                c1 = 0;
                for (int j = 0; j < contestants[i].jumps.Count; j++ )
                {
                    c1 += contestants[i].jumps[j].totalScore;
                }

                for (int j = 0; j < contestants.Count; j++)
                {
                    c2 = 0;
                    for (int f = 0; f < contestants[j].jumps.Count; f++)
                    {
                        c2 += contestants[j].jumps[f].totalScore;
                    }

                    if (c1 <= c2)
                        ;
                    else
                    {
                        Contestant temp = contestants[i];
                        contestants[i] = contestants[j];
                        contestants[j] = temp;
                    }
                }
            }
        }
        public void PrintList()
        {
            int i = 0;
            foreach (var contestant in contestants) 
            {
                i++;
                double totalScore = 0;
                for(int j = 0; j < contestant.jumps.Count; j++)
                       totalScore += contestant.jumps[j].totalScore;

                Console.WriteLine(i + ": " + contestant.id + " " + contestant.name + " with a score of : " + totalScore);
            }
        }
        public ContestantList ReadContestantsFromXMLFile(string filePath = @"c:\Contestants.xml")
        {
            ContestantList tList = new ContestantList();
            try
            {
                System.Xml.Serialization.XmlSerializer reader =
                    new System.Xml.Serialization.XmlSerializer(typeof(ContestantList));
                System.IO.StreamReader file = new System.IO.StreamReader(filePath);
                tList = (ContestantList)reader.Deserialize(file);
                file.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Data);
            }
            contestants = tList.contestants;
            return tList;
        }
        public void WriteContestantToXMLFile(string filename = @"c:\Contestants.xml")
        { 
            
            int id = 1;
            string name;
            int sex;
            string nationality;
            int age;;

            FileStream writer = new FileStream(filename, FileMode.Create);

            DataContractSerializer ser =
                new DataContractSerializer(typeof(ContestantList));

            ContestantList tempList = new ContestantList();

            while (true)
            {
                Console.WriteLine("Enter id");
                id = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter age");
                age = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter sex");
                sex = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter name");
                name = Console.ReadLine();
                Console.WriteLine("Enter nationality");
                nationality = Console.ReadLine();

                //If id == 0 break loop
                if (id != 0)
                {

                    Contestant temp = new Contestant(id, name, age, sex, nationality, 0);
                    tempList.AddContestants(temp);
                }
                else
                    break;

            }
            ser.WriteObject(writer, tempList);
            writer.Close();
        }
    }
}
